package com.app.extended_plugin.utlis;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.util.Locale;


public class NetInfoManager {

    static private String TAG = NetInfoManager.class.getSimpleName();

    private Context mContext;

    public static NetInfoManager newInstance(Context context) {
        return new NetInfoManager(context);
    }

    private NetInfoManager(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("The context must not be null.");
        }
        mContext = context;
        getNetWork(context);
    }

    /**
     * 获取 network
     * @return //460-01(CN-CMCC-4G)
     */
    public static String getNetWork(Context context) {
        String netExtra = "";
        String netNG = "";
        String simCode ="";
        TelephonyManager mTm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        Log.e(TAG,mTm.getSimOperatorName());
        Log.e(TAG,mTm.getSimOperator());
        Log.e(TAG,mTm.getSimCountryIso());

        if (mTm.getSimState() == TelephonyManager.SIM_STATE_READY) {
           String  _operator = mTm.getSimOperatorName();
            simCode = mTm.getSimOperator() +" - ";
            if (null != _operator) {
                if (_operator.equals("中国移动")) {
                    // 中国移动
                    netExtra = "(" + mTm.getSimCountryIso().toUpperCase(Locale.ROOT) +"-CMCC" + ") ";
                } else if (_operator.equals("中国联通")) {
                    // 中国联通
                    netExtra = "(" + mTm.getSimCountryIso().toUpperCase(Locale.ROOT) + "-CUCC" + ") ";
                } else if (_operator.equals("中国电信")) {
                    // 中国电信
                    netExtra = "(" + mTm.getSimCountryIso().toUpperCase(Locale.ROOT) + "-CTC" + ") ";
                } else {
                    netExtra = "(" + mTm.getSimCountryIso().toUpperCase(Locale.ROOT) + "-" +_operator + ") ";
                }
            }
        } else {
            return "SIM_NOT_READY ";
        }
        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        if(ni == null) {
            return "null";
        }
        switch (ni.getSubtype()) {

            case TelephonyManager.NETWORK_TYPE_GPRS: // 联通2g
            case TelephonyManager.NETWORK_TYPE_CDMA: // 电信2g
            case TelephonyManager.NETWORK_TYPE_EDGE: // 移动2g
            case TelephonyManager.NETWORK_TYPE_1xRTT:
            case TelephonyManager.NETWORK_TYPE_IDEN:
                netNG = " - 2G";
                break;
            case TelephonyManager.NETWORK_TYPE_EVDO_A: // 电信3g
            case TelephonyManager.NETWORK_TYPE_UMTS:
            case TelephonyManager.NETWORK_TYPE_EVDO_0:
            case TelephonyManager.NETWORK_TYPE_HSDPA:
            case TelephonyManager.NETWORK_TYPE_HSUPA:
            case TelephonyManager.NETWORK_TYPE_HSPA:
            case TelephonyManager.NETWORK_TYPE_EVDO_B:
            case TelephonyManager.NETWORK_TYPE_EHRPD:
            case TelephonyManager.NETWORK_TYPE_HSPAP:
            case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
                netNG = " - 3G";
                break;
            case TelephonyManager.NETWORK_TYPE_LTE:
            case 19:  // 聚波载合 4G+
                netNG = " - 4G";
                break;
//            case TelephonyManager.NETWORK_TYPE_NR: // 需要sdk > 28 , 暂时使用code码
            case 20:
                netNG = " - 5G";
                break;
//            case  TelephonyManager.NETWORK_TYPE_UNKNOWN:
//                netNG = "-(UNKNOWN)";
            default:
//                netNG = ni.getState() + " / " + "NET_UNKNOWN CODE: " + ni.getSubtype();
                break;
        }
        Log.d(TAG ,netExtra + netNG);

        return simCode + netExtra+ netNG;
    }
}
